#include "Stack.h"
void Stack::resize()
{
	if (_top == _capacity)
	{
		_capacity *= 2;
		ElemType* tmp = (ElemType*)malloc(sizeof(ElemType) * _capacity);
		memcpy(tmp, _value, sizeof(ElemType) * _top);
		free(_value);
		_value = tmp;
	}
}

Stack::Stack(int capacity)
	:_top(0)
	, _capacity(capacity)
	, _value((ElemType*)malloc(sizeof(ElemType)* _capacity))
{}

Stack::Stack(const Stack& st)
{
	_top = st._top;
	_capacity = st._capacity;
	_value = (ElemType*)malloc(sizeof(ElemType) * _capacity);
	memcpy(_value, st._value, sizeof(ElemType) * _top);
}

Stack& Stack::operator=(const Stack& st)
{
	_top = st._top;
	_capacity = st._capacity;
	free(_value);
	_value = (ElemType*)malloc(sizeof(ElemType) * _capacity);
	memcpy(_value, st._value, sizeof(ElemType) * _top);
	return *this;
}

Stack::~Stack()
{
	_top = _capacity = 0;
	free(_value);
}

void Stack::push(ElemType val)
{
	resize();
	_value[_top++] = val;
}

void Stack::pop()
{
	if (_top != 0)
	{
		_top--;
	}
}
int Stack::top()
{
	if (_top == 0)	return -1;
	return _value[_top - 1];
}

void Stack::Print()
{
	for (int i = 0; i < _top; ++i)
	{
		cout << _value[i] << " ";
	}
	cout << endl;
}
